package com.leetcode.problems021;


import com.leetcode.util.ListNode;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null){
            return list2;
        }
        if(list2 == null){
            return list1;
        }
        ListNode res = new ListNode(0);
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        ListNode curRes = res;
        while(cur1 != null && cur2 != null){
           if( cur1.val <= cur2.val){
               curRes.next = new ListNode(cur1.val);
               cur1 = cur1.next;
           }else{
               curRes.next = new ListNode(cur2.val);
               cur2 = cur2.next;
           }
            curRes = curRes.next;
        }
        ListNode tep = cur1 == null? cur2:cur1;
        while(tep != null){
            curRes.next = new ListNode(tep.val);
            tep = tep.next;
            curRes = curRes.next;
        }
        return res.next;
    }
    public static void main(String[] args) {
        int[] arr = {1,2,4,5};
        int[] arr1 = {1,3,4};
        ListNode.printListNode(new Solution().mergeTwoLists(ListNode.getListFormArray(arr),ListNode.getListFormArray(arr1)));
    }
}
